/*
 * Created on Dec 11, 2005
 */
package dbsucxentW;

/**
 * @author Seah Boon Siew
 */
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/** table model built from getting metadata about a table in a jdbc database 
*/ 
public class JDBCTableModel extends AbstractTableModel {
	
	Object[][] contents;
	String[] columnNames;
	Class[] columnClasses;
	
	/* modified by Lee Tzyy Ming
	public JDBCTableModel (Connection conn, String tableName, Content cont) throws SQLException {
	super();
	getTableContents (conn, tableName, cont);
	}
	*/
	
	public JDBCTableModel (ResultSet results, boolean hasEnded) throws SQLException {
		super();
		getTableContents (results, hasEnded);
	}
	
	//protected void getTableContents (Connection conn, String tableName, Content cont) // modified by Lee Tzyy Ming
	protected void getTableContents (ResultSet results, boolean hasEnded) 
	throws SQLException {
		/* removed by Lee Tzyy Ming
		Statement statement = conn.createStatement ();
		
		ResultSet results = statement.executeQuery ("DBCC DROPCLEANBUFFERS;\nDBCC FREEPROCCACHE;\n" + tableName); 
		*/

		// get metadata: what columns exist and what
		// types (classes) are they?		
		System.out.println("getTable start");
		results.beforeFirst();	// added by Lee Tzyy Ming
		ResultSetMetaData rsmd = results.getMetaData();
	    int numberOfColumns = rsmd.getColumnCount();
	    
	
		System.out.println ("got column results" + results.getWarnings());
		ArrayList colNamesList = new ArrayList();
		ArrayList colClassesList = new ArrayList();
		columnNames = new String [numberOfColumns + 1];
		
		columnNames[0] = "Row";
		colClassesList.add (Integer.class);
		
		for ( int i = 1; i <= numberOfColumns; i++){
	
			int dbType = rsmd.getColumnType(i);
			
			switch (dbType) {
				case Types.INTEGER:
				colClassesList.add (Integer.class); break; 
				case Types.FLOAT:
				colClassesList.add (Float.class); break; 
				case Types.DOUBLE: 
				case Types.REAL:
				colClassesList.add (Double.class); break; 
				case Types.DATE: 
				case Types.TIME: 
				case Types.TIMESTAMP:
				colClassesList.add (java.sql.Date.class); break;
				case Types.BIGINT:								// added by Lee Tzyy Ming
				default:
				colClassesList.add (String.class); break; 
			};
						
			columnNames[i] = rsmd.getColumnName(i);
		}
		
		columnClasses = new Class [colClassesList.size()];
		colClassesList.toArray (columnClasses);
		
		// get all data from table and put into
		// contents array
		
	    ArrayList rowList = new ArrayList();
		int row = 1;
		while (results.next()) {
		ArrayList cellList = new ArrayList();
		
		Object cellValue = new Integer(row++);
		cellList.add (cellValue);
		
		for (int i = 1; i < columnClasses.length; i++) { 
			cellValue = null;
			
			if (columnClasses[i] == String.class)
				if (results.getString (columnNames[i]) != null)
					cellValue = results.getString (columnNames[i]).trim();
				else
					cellValue = results.getString (columnNames[i]);
			else if (columnClasses[i] == Integer.class) 
			cellValue = new Integer ( 
			results.getInt (columnNames[i])); 
			else if (columnClasses[i] == Float.class) 
			cellValue = new Float ( 
			results.getInt (columnNames[i])); 
			else if (columnClasses[i] == Double.class) 
			cellValue = new Double ( 
			results.getDouble (columnNames[i]));
			else if (columnClasses[i] == java.sql.Date.class) 
			cellValue = results.getDate (columnNames[i]); 
			else 
			System.out.println ("Can't assign " + columnNames[i]);
			cellList.add (cellValue);
		}// for
		
		Object[] cells = cellList.toArray();
		rowList.add (cells);
	
		if (hasEnded == true){
			//results.close();				// } removed by Lee Tzyy Ming
			//statement.close();			// }
			String[] rowd = new String[2];
    		rowd[0]	= new String("NA");
    		rowd[1]	= new String("No Rows Returned");
    		String[][] emptyContent = new String[1][];
    		emptyContent[0] = rowd;
    		contents = emptyContent;
			System.out.println ("Aborted");
			return;
		}
		
		} // while
		
		// finally create contents two-dim array
		contents = new Object[rowList.size()] [];
		for (int i=0; i<contents.length; i++)
		
		contents[i] = (Object []) rowList.get (i);
		System.out.println ("Created model with " + contents.length + " rows");
		
		// close stuff
		//results.close();					// } removed by Lee Tzyy Ming
		//statement.close();				// }
		
	    if (contents.length == 0){
    		String[] rowd = new String[2];
    		rowd[0]	= new String("NA");
    		rowd[1]	= new String("No Rows Returned");
    		String[][] emptyContent = new String[1][];
    		emptyContent[0] = rowd;
    		contents = emptyContent;
    		return;
	    }
    
	
	}
	// AbstractTableModel methods
	public int getRowCount() {
		return contents.length;
	}
	
	public int getColumnCount() {
		if (contents.length == 0)
			return 0;
		else
			return contents[0].length;
	}
	
	public Object getValueAt (int row, int column) {
		return contents [row][column];
	}
	
	public Class getColumnClass (int col) {
		return columnClasses [col];
	}
	
	public String getColumnName (int col) { 
		return columnNames [col]; 
	} 

}